package biz.bbtec.sbm.store.action;

import java.util.List;
import java.util.Map;
import java.util.ArrayList;
import java.util.HashMap;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

import biz.bbtec.sbm.store.bean.CategoryBean;
import biz.bbtec.sbm.store.factory.CategoryFactory;
import biz.bbtec.sbm.store.service.CategoryService;
import biz.bbtec.sbm.user.bean.UserBean;

public class CategoryAction extends ActionSupport {

	private CategoryBean catbean = new CategoryBean();
	private int catid;
	private Map<Integer, List<CategoryBean>> catmap = new HashMap<Integer, List<CategoryBean>>();
	private List<CategoryBean> first = new ArrayList<CategoryBean>();

	public List<CategoryBean> getFirst() {
		return first;
	}

	public void setFirst(List<CategoryBean> first) {
		this.first = first;
	}

	public Map<Integer, List<CategoryBean>> getCatmap() {
		return catmap;
	}

	public void setCatmap(Map<Integer, List<CategoryBean>> catmap) {
		this.catmap = catmap;
	}

	public int getCatid() {
		return catid;
	}

	public void setCatid(int catid) {
		this.catid = catid;
	}

	public CategoryBean getCatbean() {
		return catbean;
	}

	public void setCatbean(CategoryBean catbean) {
		this.catbean = catbean;
	}

	public String AddCategory() {
		CategoryService catservice = CategoryFactory.getInstance()
				.creattemService();
		Map session = ActionContext.getContext().getSession();
		UserBean user = (UserBean) session.get("user");
		if (!(catservice.addcat(catbean, user))) {
			this.addActionError("您要添加的类别已存在！");
			return INPUT;
		}
		this.addActionError("添加类别成功！");
		return SUCCESS;
	}

	public String showcat() {
		CategoryService catservice = CategoryFactory.getInstance()
				.creattemService();
		Map session = ActionContext.getContext().getSession();
		UserBean user = (UserBean) session.get("user");
		first = catservice.showfirst(user);
		if (first.size() == 0) {
			this.addActionError("请先添加类别！");
			return INPUT;
		}
		for (int i = 0; i < first.size(); i++) {
			List<CategoryBean> second = new ArrayList<CategoryBean>();
			second = catservice.showsecond(user, first.get(i).getCatid());
			if (second.size() != 0) {
				catmap.put(first.get(i).getCatid(), second);
			}
		}
		if (catmap.size() == 0) {
			this.addActionError("请先添加类别！");
			return INPUT;
		}
		return SUCCESS;
	}
}
